﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Linq;

namespace batchMail
{
    class MapEmail
    {


        /// <summary>
        /// 读取txt文件，获得发送email的列表
        /// </summary>
        /// <param name="path">txt文件的路径</param>
        /// <returns></returns>
        public static List<MapEmail> FromTxtToMapEmail(string path=@".\data.txt") 
        {

            if (path==null)
            {
                path = @".\data.txt";
            }

            if (!File.Exists(path))
            {
                return null;
            }


            StreamReader sReader = new StreamReader(path, Encoding.Default);

            string txt = sReader.ReadToEnd();

            string[] txtArray = txt.Split("\r\n".ToCharArray());

            sReader.Close();
            sReader.Dispose();
            sReader = null;

            var dataList = from str in txtArray
                           where str!=null&&str!=""
                           select new MapEmail
                           {
                               UnitId = str.Split('\t')[0],
                               Email = str.Split('\t')[1]
                           };

            return dataList.ToList();


        }



        /// <summary>
        /// 单位编号
        /// </summary>
        public string UnitId { get; set; }


        /// <summary>
        /// 发送的EMail
        /// </summary>
        public string Email { get; set; }


        public MapEmail()
        {

        }


        public MapEmail(string unitId,string email)
        {
            this.UnitId = unitId;
            this.Email = email;
        }


    }
}
